X-Git-Url: https://git.r.bdr.sh/rbdr/super-polarity/blobdiff_plain/f8aec187ea7dc410a32996406109f290f3199ffa..2af83e98005a14c439b360a5b9ac636f594d9f0c:/Super%20Polarity/Actors/Actor.cs diff --git a/Super Polarity/Actors/Actor.cs b/Super Polarity/Actors/Actor.cs new file mode 100644 index 0000000..4351bf2 --- /dev/null +++ b/Super Polarity/Actors/Actor.cs @@ -0,0 +1,161 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; + +namespace SuperPolarity +{ + class Actor + { + protected Game game; + + // Graphics / In-Game + protected Texture2D Texture; + protected Vector2 Origin; + public bool Active; + + // Physical Properties + public Vector2 Position; + protected Vector2 Velocity; + protected Vector2 Acceleration; + protected float Angle; + + // Constraints / Behavior + protected float MaxVelocity; + protected float AccelerationRate; + + public int Width + { + get { return Texture.Width; } + } + + public int Height + { + get { return Texture.Height; } + } + + public Actor(Game newGame) + { + game = newGame; + } + + public virtual void Initialize(Texture2D texture, Vector2 position) + { + Texture = texture; + Position = position; + Active = true; + + Origin = new Vector2(Texture.Width / 2, Texture.Height / 2); + Velocity = new Vector2(0, 0); + Acceleration = new Vector2(0, 0); + + MaxVelocity = 5; + AccelerationRate = 10; + } + + public void AutoDeccelerate(GameTime gameTime) + { + if (Acceleration.X == 0 && Velocity.X > 0) + { + if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.X) + { + Velocity.X = 0; + Acceleration.X = 0; + } + else + { + Acceleration.X = -AccelerationRate; + } + } + + if (Acceleration.X == 0 && Velocity.X < 0) + { + if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.X) + { + Velocity.X = 0; + Acceleration.X = 0; + } + else + { + Acceleration.X = AccelerationRate; + } + } + + if (Acceleration.Y == 0 && Velocity.Y > 0) + { + if (AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds > Velocity.Y) + { + Velocity.Y = 0; + Acceleration.Y = 0; + } + else + { + Acceleration.Y = -AccelerationRate; + } + } + + if (Acceleration.Y == 0 && Velocity.Y < 0) + { + if (-AccelerationRate * gameTime.ElapsedGameTime.TotalSeconds < Velocity.Y) + { + Velocity.Y = 0; + Acceleration.Y = 0; + } + else + { + Acceleration.Y = AccelerationRate; + } + } + } + + public virtual void Update(GameTime gameTime) + { + Move(gameTime); + ChangeAngle(); + } + + public void Move(GameTime gameTime) + { + AutoDeccelerate(gameTime); + + Velocity.X = Velocity.X + Acceleration.X * (float)gameTime.ElapsedGameTime.TotalSeconds; + Velocity.Y = Velocity.Y + Acceleration.Y * (float)gameTime.ElapsedGameTime.TotalSeconds; + + if (Velocity.X > MaxVelocity) + { + Velocity.X = MaxVelocity; + } + + if (Velocity.X < -MaxVelocity) + { + Velocity.X = -MaxVelocity; + } + + if (Velocity.Y > MaxVelocity) + { + Velocity.Y = MaxVelocity; + } + + if (Velocity.Y < -MaxVelocity) + { + Velocity.Y = -MaxVelocity; + } + + Position.X = Position.X + Velocity.X; + Position.Y = Position.Y + Velocity.Y; + } + + public void ChangeAngle() + { + Angle = (float)Math.Atan2(Velocity.Y, Velocity.X); + } + + public virtual void Draw(SpriteBatch spriteBatch) + { + spriteBatch.Draw(Texture, Position, null, Color.White, Angle, Origin, 1f, SpriteEffects.None, 0f); + } + } +}